<template>
  <div>
    <el-form :inline="true" :model="searchEntity" class="demo-form-inline">
      <el-form-item label="品牌名称">
        <el-input v-model="searchEntity.name" placeholder="品牌名称"></el-input>
      </el-form-item>
      <el-form-item label="品牌首字母">
        <el-input
          v-model="searchEntity.firstChar"
          placeholder="品牌首字母"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="openAdd"
          >添加</el-button
        > </el-form-item
      ><br />
      <el-button type="success" @click="updateData">批量删除</el-button>
      <el-button @click="allCheck">全选</el-button>
      <el-button @click="allNoCheck">全不选</el-button>
      <el-button @click="reverseCheck">反选</el-button>
    </el-form>
    <el-table
      ref="multipleTable"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="品牌ID" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品牌名称" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品牌首字母" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.firstChar }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchEntity.startPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="searchEntity.endPage"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalPage"
    >
    </el-pagination>
    <div>
      <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center
      >
        品牌名称：<el-input
          v-model="brandEntity.name"
          placeholder="请输入内容"
        ></el-input>
        <el-input
          v-model="brandEntity.firstChar"
          placeholder="请输入内容"
        ></el-input>
        <span slot="footer" class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="beSure()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <div>
      <el-dialog
        :title="formTitle"
        :visible.sync="dialogTableVisible"
        width="600px"
      >
        {{ specificationEntity.specificationOptionList }}
        <el-form ref="form" :model="specificationEntity" label-width="100px">
          <el-form-item label="规格名称：">
            <el-input v-model="specificationEntity.specName"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addOptions"
              >新增规格选项</el-button
            >
          </el-form-item>
        </el-form>
        <el-table :data="specificationEntity.specificationOptionList">
          <el-table-column label="规格选项">
            <template #default="scope">
              <el-input
                v-model="scope.row.optionName"
                placeholder="请输入内容"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="排序">
            <template #default="scope">
              <el-input
                v-model="scope.row.orders"
                placeholder="请输入内容"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column property="orders" label="操作">
            <template #default="scope">
              <el-button type="danger" plain @click="delOptions(scope.$index)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div>
          <el-button type="primary" @click="saveUpdate">主要按钮</el-button>
          <el-button type="success">成功按钮</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  brandList,
  update,
  findByid,
  save,
  remove,
  delByIds,
} from "@/api/brandApi";
import { add } from "@/api/brandApi";
export default {
  data() {
    return {
      dialogTableVisible: false,
      centerDialogVisible: false,
      specificationEntity: {
        id: null,
        specName: "",
        specificationOptionList: [],
      },
      formTitle: "",
      brandEntity: {
        id: "",
        name: "",
        firstChar: "",
      },
      searchEntity: {
        name: "",
        firstChar: "",
        startPage: "1",
        endPage: "5",
      },
      totalPage: "",
      tableData: [],
      ids: [],
    };
  },
  created() {
    this.list();
  },
  methods: {
    saveUpdate() {
      add(this.specificationEntity).then((result) => {
        this.specificationEntity.id = null;
        this.specificationEntity.specName = "";
        this.specificationEntity.specificationOptionList = [];
        this.centerDialogVisible = false;
        this.list();
      });
    },
    delOptions(index) {
      console.log(index);
      this.specificationEntity.specificationOptionList.splice(index, 1);
    },
    addOptions() {
      this.specificationEntity.specificationOptionList.push({});
    },
    openAdd() {
      (this.specificationEntity.id = null),
        (this.specificationEntity.specName = ""),
        (this.specificationEntity.specificationOptionList = []),
        (this.dialogTableVisible = true);
      this.formTitle = "添加";
    },
    updateData() {
      delByIds(this.ids);
      this.list();
    },
    handleSelectionChange(val) {
      this.ids = [];
      if (val.length > 0) {
        val.forEach((row) => {
          this.ids.push(row.id);
        });
      } else {
        this.ids = [];
      }
    },
    allCheck() {
      this.tableData.forEach((row) => {
        this.$refs.multipleTable.toggleRowSelection(row, true);
      });
    },
    allNoCheck() {
      this.tableData.forEach((row) => {
        this.$refs.multipleTable.toggleRowSelection(row, false);
      });
    },
    reverseCheck() {
      this.tableData.forEach((row) => {
        this.$refs.multipleTable.toggleRowSelection(row);
      });
    },
    beSure() {
      if (this.brandEntity.id == null) {
        save(this.brandEntity);
        this.brandEntity = {};
        this.centerDialogVisible = false;
        this.list();
      } else {
        update(this.brandEntity);
        this.brandEntity = {};
        this.centerDialogVisible = false;
        this.list();
      }
    },
    list() {
      brandList(this.searchEntity).then((result) => {
        this.tableData = result.data.data.list;
        this.totalPage = result.data.data.total;
      });
    },
    handleSizeChange(val) {
      this.searchEntity.endPage = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.searchEntity.startPage = val;
      this.list();
    },
    handleEdit(index, row) {
      findByid(row.id).then((result) => {
        this.brandEntity = result.data.data;
        this.centerDialogVisible = true;
      });
    },
    handleDelete(index, row) {
      remove(row.id);
      this.list();
    },
    onSubmit() {
      this.list();
    },
  },
};
</script>

<style scoped></style>
